package com.witmore.skyline.web.controller;

import com.witmore.skyline.web.controller.enums.WitmoreErrorCode;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.utils.mapworld.MapWorldGeocoder;
import com.witmore.skyline.web.utils.mapworld.MapWorldReverseGeocoding;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 经纬度地址相互转换 2.0
 * Author: 豆子高
 * Date: 2025/4/17 14:01
 */
@Slf4j
@RestController
@RequestMapping("api/geocode/")
public class GeocodeController {

    /**
     * 正向地址转换（地址转换成经纬度） 2.0
     */
    @PostMapping(name = "正向地址转换（地址转换成经纬度）", value = "geocode")
    public ResponseResult<Map<String, Double>> geocoding(@RequestParam("address") String address) {
        try {
            return ResponseResult.success(MapWorldGeocoder.geocode(address));
        } catch (Exception e) {
            log.error("获取经纬度失败", e);
            return ResponseResult.fail(WitmoreErrorCode.GEOCODE_ERROR);
        }
    }

    /**
     * 逆向地址转换（经纬度转换成地址） 2.0
     */
    @PostMapping(name = "逆向地址转换（经纬度转换成地址）", value = "reverseGeocode")
    public ResponseResult<String> reverseGeocode(@RequestParam("lng") double lng, @RequestParam("lat") double lat) {
        try {
            return ResponseResult.success(MapWorldReverseGeocoding.reverseGeocode(lng, lat));
        } catch (Exception e) {
            log.error("获取地址失败", e);
            return ResponseResult.fail(WitmoreErrorCode.GEOCODE_ERROR);
        }
    }
}
